<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div @click="logohome">
        <img src="../assets/heima.png" alt="" />
        <span>vue-admin-awei</span>
      </div>
      <div>
        <!-- <el-button type="success" @click="logohome">首页</el-button>
        <el-button type="info" @click="logout">退出</el-button> -->

        <el-dropdown @command="handleCommand">
          <div class="userInfo">
            <el-avatar
              src="https://ts1.cn.mm.bing.net/th/id/R-C.d99e9cdc1c54945672dadab879534d72?rik=PdSNDb0CwAZk1g&riu=http%3a%2f%2fcomic.sinaimg.cn%2f2011%2f0921%2fU5238P1157DT20110921163640.jpg&ehk=jB1jIDlUpHmfdu0vy4GhRF1WJ4DQ9vjn%2bL8fNST4nn4%3d&risl=&pid=ImgRaw&r=0&sres=1&sresct=1"
            ></el-avatar>
            <span style="color:#fff">admin</span>
            <i class="el-icon-arrow-down el-icon--right"></i>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="personal">个人中心</el-dropdown-item>
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>

      </div>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333333"
          text-color="#fff"
          active-text-color="#fff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="$router.path"
          @open="open"
        >
          <el-menu-item index="/welcome">
            <!-- 图标 -->
            <i class="iconfont el-icon-s-home"></i>
            <!-- 文本 -->
            <span>首页</span>
          </el-menu-item>
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.path+''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconsObj: {
        125: "iconfont el-icon-user-solid",
        103: "iconfont el-icon-s-check",
        101: "iconfont el-icon-s-goods",
        102: "iconfont el-icon-s-claim",
        145: "iconfont el-icon-s-data",
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: "",
      activeIndex: ""
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    open(activeIndex) {
      this.activeIndex = activeIndex
    },
    handleCommand(command) {
        switch (command) {
          case 'personal':
            break;
          case 'logout':
            this.logout()
        }
    },
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    logohome() {
      this.$router.push("/home");
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    margin-left: 5px;
    display: flex;
    align-items: center;
    img{
      width: 40px;
      height: 40px;
    }
    span {
      margin-left: 5px;
    }
  }
}

.el-aside {
  background-color: #333333;
  box-shadow:1px 0px 13px 0px rgba(50, 50, 50, 0.75);
  z-index: 1000;
  transition:width 0.5s ease;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.iconfont{
  font-size: 20px;
}

.userInfo{
  display: flex;
  align-items: center;
  .el-avatar{
    margin-right: 5px;
  }
  i{
    color:#fff
  }
}

    //设置了默认左边框为白色
    // .el-submenu .el-submenu__title{
    //     border-left: #fff solid 6px;
    // }
    设置鼠标悬停时el-submenu的样式
    // .el-submenu .el-submenu__title:hover{
    //     background-color: #41b584 !important;
    //     color: #fff !important;
    //     i {
    //         color: #fff;
    //     }
    // }

    //设置鼠标悬停时el-menu-item的样式
    .el-menu-item:hover{
        background-color: #41b584 !important;
        color: #fff !important;
        //less语法，实现鼠标悬停时icon变色
        i {
            color: #fff;
        }
    }
  //设置选中el-menu-item时的样式
    .el-menu-item.is-active {
        background-color: #41b584 !important;
        color: #fff !important;
    }
</style>
